package com.base.controller;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.base.log.RequestUtils;
import com.base.po.voip.DialogBox;
import com.base.self.service.ISelfOrderService;
import com.base.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.sqlparam.UsersExamplePage;
import com.base.po.voip.Users;
import com.base.po.voip.UsersExample.Criteria;
import com.base.utils.IP;
import com.base.utils.Page;
import com.base.utils.PasswordUtils;

/**
 * admin管理系统控制器
 *
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_shangjiaguanli {

	private final LogsService logsService;// log日志

	private final UsersService usersService;

	private final SubscribersService subscribersService;

	private final CallbackinfoesService callbackinfoesService;// 话单记录

	private final DialogBoxService dialogBoxService;

	private final ISelfOrderService iSelfOrderService;

	private final RequestUtils requestUtils;

	public content_shangjiaguanli(LogsService logsService,
								  UsersService usersService,
								  SubscribersService subscribersService,
								  CallbackinfoesService callbackinfoesService,
								  DialogBoxService dialogBoxService,
								  ISelfOrderService iSelfOrderService,
								  RequestUtils requestUtils) {
		this.logsService = logsService;
		this.usersService = usersService;
		this.subscribersService = subscribersService;
		this.callbackinfoesService = callbackinfoesService;
		this.dialogBoxService = dialogBoxService;
		this.iSelfOrderService = iSelfOrderService;
		this.requestUtils = requestUtils;
	}

	// 商家管理页
	@RequestMapping("/content_shangjiaguanliOld")
	public ModelAndView indexOld(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		UsersExamplePage examplePage = new UsersExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = 0;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		examplePage.setOrderByClause("CreateTime");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<Users> users = null;
		Criteria criteria = examplePage.createCriteria();
		List<String> list = null;
		switch (adminUsergrade) {
		case 0:
			examplePage.getOredCriteria().add(criteria.andRoleEqualTo(2));
			break;
		case 1:
			// 查询OEMID下的用户
			// 查询OEM下的商户以及OEM
			// 查询OEM id
			list = usersService.selectByParentId(adminUserId);
			for (String oemId : list) {
				// OEM下的商家
				examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId).andRoleEqualTo(2));
			}
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId).andRoleEqualTo(2));
			break;
		case 2:
			//当前商家
			Users shop = usersService.selectByPrimaryKey(adminUserId);
			//当前商家的上级oem
			list = usersService.selectByShopId(shop.getId());
            for (String oemId : list) {
				// OEM下的商家
				examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId).andRoleEqualTo(2).andShopidEqualTo(adminUserId));
			}
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(shop.getParentid()).andRoleEqualTo(2).andShopidEqualTo(adminUserId));
			break;
		}
		/*
		 * 设置分页
		 */
		int totlenum = usersService.countByExample(examplePage);// 页面总数
		request.setAttribute("totlenum", totlenum);// 总数据传入session中
		request.setAttribute("curPage", 1);// 当前页面传入session中
		users = usersService.selectByExamplePage(examplePage);
		if (users != null) {
			for (Users u : users) {
				// 设置系统消耗
				BigDecimal xh = null;
				if (u.getRole() == 1) {
					xh = callbackinfoesService.sumOmoneyParentID(u.getId());
				} else if (u.getRole() == 2) {
					xh = callbackinfoesService.sumOmoneyShopID(u.getId());
				}
				if (xh == null) {
					u.setKeypath("0");
				} else {
					u.setKeypath(xh + "");
				}
				u.setPassword(PasswordUtils.decodePWD(u.getPassword()));// 设置明文密码
				// 设置OEM以及所属商家的账户名
				Users parent = usersService.selectByPrimaryKey(u.getParentid());
				Users shop = usersService.selectByPrimaryKey(u.getShopid());
				if (parent != null && parent.getUsername() != null) {
					u.setParentid(parent.getUsername());
				} else {
					u.setParentid("未知");
				}

				if (shop != null && shop.getUsername() != null) {
					u.setShopid(shop.getUsername());
				} else {
					u.setShopid("未知");
				}

			}
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_shangjiaguanli");
			request.setAttribute("auth", 4);
			request.setAttribute("list", users);
			return modelAndView;
		}
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		logsService.insert(CommContent.TYPE_SYS, CommContent.LOGS_READ + "流量代理商信息", usernamesys, true,
				IP.getIpAddr(request));
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}

	@RequestMapping("/selectShopOld")
	public ModelAndView selectShopOld(HttpServletRequest request, String id, String username, Integer datastatus,
			Integer curPage, String shopid, String parentid) throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		if (id == null) {
			if (request.getSession().getAttribute("id") != null) {
				id = (String) request.getSession().getAttribute("id");
			}
		}
		if (username == null) {
			if (request.getSession().getAttribute("username") != null) {
				username = (String) request.getSession().getAttribute("username");
			}
		}
		if (shopid == null) {
			if (request.getSession().getAttribute("shopid") != null) {
				shopid = (String) request.getSession().getAttribute("shopid");
			}
		}
		if (parentid == null) {
			if (request.getSession().getAttribute("parentid") != null) {
				parentid = (String) request.getSession().getAttribute("parentid");
			}
		}
		if (datastatus == null) {
			if (request.getSession().getAttribute("datastatus") != null) {
				datastatus = (Integer) request.getSession().getAttribute("datastatus");
			} else {
				datastatus = 0;
			}
		}

		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<Users> users = null;
		//choose = 2 默认分页查询
		Integer choose = 2;
		if(adminUsergrade < 2) {
			if(id != null &&! id.equals("") || username != null &&! username.equals("") || shopid != null &&! shopid.equals("")||
					parentid != null &&! parentid.equals("") || datastatus != 0  ) {
				choose = 1;
			}
		}

		if(adminUsergrade == 2) {
			if(id != null &&! id.equals("") || username != null &&! username.equals("") ||
				datastatus != 0  ) {
				choose = 1;
			}
		}

		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		UsersExamplePage examplePage = new UsersExamplePage();
		examplePage.setOrderByClause("CreateTime");
		examplePage.pagesize = 10;
		examplePage.start = (curPage - 1) * examplePage.pagesize;
		examplePage.rows = examplePage.pagesize;// 设置首次分页


		Criteria criteria = examplePage.createCriteria();
		// 条件筛选
		if(adminUsergrade == 0) {
			if (id != null && !id.equals("")) {
				request.setAttribute("id", id);
				criteria.andRoleEqualTo(2).andIdEqualTo(id);
				examplePage.getOredCriteria().add(criteria);
			}
			if (username != null && !username.equals("")) {
				request.setAttribute("username", username);
				criteria.andRoleEqualTo(2).andUsernameLike("%" + username + "%");
				examplePage.getOredCriteria().add(criteria);
			}
			if (parentid != null && !parentid.equals("")) {
				request.setAttribute("parentid", parentid);
				Users agent = usersService.selectByUsername(parentid.trim());
				if (agent != null) {
					criteria.andParentidEqualTo(agent.getId());
					examplePage.or(examplePage.createCriteria().andRoleEqualTo(2).andParentidEqualTo(agent.getId()));
					examplePage.getOredCriteria().add(criteria);
				}
			}
			if (shopid != null && !shopid.equals("")) {
				request.setAttribute("shopid", shopid);
				Users shop = usersService.selectByUsername(shopid.trim());
				if (shop != null) {
					criteria.andShopidEqualTo(shop.getId());
					examplePage.or(examplePage.createCriteria().andRoleEqualTo(2).andShopidEqualTo(shop.getId()));
					examplePage.getOredCriteria().add(criteria);
				}
			}
			if (datastatus != null) {
				// 判断当前可用性
				switch (datastatus) {
				case 2:
					examplePage.getOredCriteria().add(criteria.andDatastatusEqualTo(false));
					break;
				case 1:
					examplePage.getOredCriteria().add(criteria.andDatastatusEqualTo(true));
					break;
				case 0:
					break;
				}
			}
		}

		if(adminUsergrade == 1) {
			List<String> list = null;
			list = usersService.selectByParentId(adminUserId);
			if (id != null && !id.equals("")) {
				request.setAttribute("id", id);
				for (String oemId : list) {
					examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId)
							.andRoleEqualTo(2).andIdEqualTo(id));
				}
				examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId)
						.andRoleEqualTo(2).andIdEqualTo(id));
			}
			if (username != null && !username.equals("")) {
				request.setAttribute("username", username);
				for (String oemId: list) {
					examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId)
							.andRoleEqualTo(2).andUsernameLike("%" + username + "%"));
				}
				examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId)
						.andRoleEqualTo(2).andUsernameLike("%" + username + "%"));
			}
			if (parentid != null && !parentid.equals("")) {
				request.setAttribute("parentid", parentid);
				Users agent = usersService.selectByUsername(parentid.trim());
				if (agent != null) {

					examplePage.getOredCriteria().add(criteria
							.andRoleEqualTo(2).andParentidEqualTo(agent.getId()));
				}
			}
			if (shopid != null && !shopid.equals("")) {
				request.setAttribute("shopid", shopid);
				Users shop = usersService.selectByUsername(shopid.trim());
				if (shop != null) {

					examplePage.getOredCriteria().add(criteria
							.andRoleEqualTo(2).andShopidEqualTo(shop.getId()));
				}
//				if(shop != null) {
//					for (Users u : list) {
//						examplePage.or(examplePage.createCriteria().andParentidEqualTo(shop.getId())
//								.andRoleEqualTo(2));
//						//查询商家下的商家
//						shopExample.getOredCriteria().add(userExample.createCriteria().andParentidEqualTo(u.getId()));
//						List<Users> shops = usersService.selectByExample(shopExample);
//						for(Users s : shops) {
//							examplePage.or(examplePage.createCriteria().andParentidEqualTo(s.getId())
//									.andRoleEqualTo(2));
//						}
//					}
//					examplePage.getOredCriteria().add(criteria.andParentidEqualTo(shop.getId()));
//				}
			}
			if (datastatus != null) {
				// 判断当前可用性
				switch (datastatus) {
				case 2:
					for (String oemId : list) {
						examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId)
								.andRoleEqualTo(2).andDatastatusEqualTo(false));
					}
					examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId)
							.andRoleEqualTo(2).andDatastatusEqualTo(false));
					break;
				case 1:
					for (String oemId : list) {
						examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId)
								.andRoleEqualTo(2).andDatastatusEqualTo(true));
					}
					examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId)
							.andRoleEqualTo(2).andDatastatusEqualTo(true));
					break;
				case 0:
					break;
				}
			}
		}

		if(adminUsergrade == 2) {
			List<String> list = null;
			//当前商家
			Users shop = usersService.selectByPrimaryKey(adminUserId);
			//当前商家的上级oem
			list = usersService.selectByShopId(shop.getId());
            if (id != null && !id.equals("")) {
				request.setAttribute("id", id);
				for (String oemId : list) {
					// OEM下的商家
					examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId)
							.andRoleEqualTo(2).andShopidEqualTo(adminUserId));
				}
				examplePage.getOredCriteria().add(criteria.andParentidEqualTo(shop.getParentid())
						.andRoleEqualTo(2).andShopidEqualTo(adminUserId));
			}
			if (username != null && !username.equals("")) {
				request.setAttribute("username", username);
				for (String oemId : list) {
					examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId)
							.andRoleEqualTo(2).andUsernameLike("%" + username + "%").andShopidEqualTo(adminUserId));
				}
				examplePage.getOredCriteria().add(criteria.andParentidEqualTo(shop.getParentid())
						.andRoleEqualTo(2).andUsernameLike("%" + username + "%").andShopidEqualTo(adminUserId));
			}
			if (datastatus != null) {
				// 判断当前可用性
				switch (datastatus) {
				case 2:
					for (String oemId : list) {
						examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId)
								.andRoleEqualTo(2).andDatastatusEqualTo(false).andShopidEqualTo(adminUserId));
					}
					examplePage.getOredCriteria().add(criteria.andParentidEqualTo(shop.getParentid())
							.andRoleEqualTo(2).andDatastatusEqualTo(false).andShopidEqualTo(adminUserId));
					break;
				case 1:
					for (String oemId : list) {
						examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId)
								.andRoleEqualTo(2).andDatastatusEqualTo(true).andShopidEqualTo(adminUserId));
					}
					examplePage.getOredCriteria().add(criteria.andParentidEqualTo(shop.getParentid())
							.andRoleEqualTo(2).andDatastatusEqualTo(true).andShopidEqualTo(adminUserId));
					break;
				case 0:
					break;
				}
			}
		}

		request.setAttribute("datastatus", datastatus);
		System.out.println(choose);
		if(choose == 2) {
			List<String> list = null;
			switch (adminUsergrade) {
			case 0:
				examplePage.getOredCriteria().add(criteria.andRoleEqualTo(2));
				break;
			case 1:
				list = usersService.selectByParentId(adminUserId);
				for (String oemId : list) {
					// OEM下的商家
					examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId).andRoleEqualTo(2));
				}
				examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId).andRoleEqualTo(2));
				break;
			case 2:
				Users shop = usersService.selectByPrimaryKey(adminUserId);
				//当前商家的上级oem
				list = usersService.selectByShopId(shop.getId());
				for (String oemId : list) {
					// OEM下的商家
					examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId).andRoleEqualTo(2).andShopidEqualTo(adminUserId));
				}
				examplePage.getOredCriteria().add(criteria.andParentidEqualTo(shop.getParentid()).andRoleEqualTo(2).andShopidEqualTo(adminUserId));
				break;
			}

		}
		/*
		 * 设置分页
		 */
		int totlenum = usersService.countByExample(examplePage);// 页面总数
		request.setAttribute("totlenum", totlenum);// 总数据传入session中
		request.setAttribute("curPage", curPage);// 当前页面传入session中

		users = usersService.selectByExamplePage(examplePage);
		if (users != null) {
			for (Users u : users) {
				// 设置系统消耗
				BigDecimal xh = null;
				if (u.getRole() == 1) {
					xh = callbackinfoesService.sumOmoneyParentID(u.getId());
				} else if (u.getRole() == 2) {
					xh = callbackinfoesService.sumOmoneyShopID(u.getId());
				}
				if (xh == null) {
					u.setKeypath("0");
				} else {
					u.setKeypath(xh + "");
				}
				u.setPassword(PasswordUtils.decodePWD(u.getPassword()));// 设置明文密码
				// 设置OEM以及所属商家的账户名
				Users parent = usersService.selectByPrimaryKey(u.getParentid());
				Users shop = usersService.selectByPrimaryKey(u.getShopid());
				if (parent != null && parent.getUsername() != null) {
					u.setParentid(parent.getUsername());
				} else {
					u.setParentid("未知");
				}

				if (shop != null && shop.getUsername() != null) {
					u.setShopid(shop.getUsername());
				} else {
					u.setShopid("未知");
				}
			}
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_shangjiaguanli");
			request.setAttribute("auth", datastatus);
			request.setAttribute("list", users);
			request.setAttribute("adminUserId", adminUserId);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}

	// 商家管理跳转
	@RequestMapping("/content_shangjiaguanli")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();

		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		// 登陆商家
		Users user = usersService.selectByPrimaryKey(adminUserId);
		List<Users> users = null;

		switch (adminUsergrade) {
		case 0:
			page.setCurrentPageNo(1);
			page.setRecordTotal(usersService.countByAll(null, null, null, null));
			users = usersService.selectByAll(page, null, null, null, null);
			break;
		case 1:
			// 查询OEMID下的用户
			// 查询OEM下的商户以及OEM
			// 查询OEM id
			page.setCurrentPageNo(1);
			page.setRecordTotal(usersService.countByParentID(adminUserId, null, null, null, null));
			users = usersService.selectByParentID(page, adminUserId, null, null, null, null);
			break;
		case 2:
			//当前商家的上级oem
			page.setCurrentPageNo(1);
			page.setRecordTotal(usersService.countByShopIDNotAll(adminUserId, null, null, null, null));
			users = usersService.selectByShopIDNotAll(page, adminUserId, null, null, null, null);
			break;
		}
		/*
		 * 设置分页
		 */
		request.setAttribute("totlenum", page.getRecordTotal()); // 分页
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
		if (users != null) {
			for (Users u : users) {
				DecimalFormat df = new DecimalFormat("#0.00");
				// 设置系统消耗
				BigDecimal xh = null;
				if (u.getRole() == 1) {
					xh = callbackinfoesService.sumOmoneyParentID(u.getId());
				} else if (u.getRole() == 2) {
					xh = callbackinfoesService.sumOmoneyShopID(u.getId());
				}
				if (xh == null) {
					u.setKeypath("0");
				} else {
					u.setKeypath(df.format(xh) + "");
				}
				u.setBlance(new BigDecimal(df.format(u.getBlance())));
				u.setPassword(PasswordUtils.decodePWD(u.getPassword()));// 设置明文密码
				// 设置OEM以及所属商家的账户名
				Users parent = usersService.selectByPrimaryKey(u.getParentid());
				Users shop = usersService.selectByPrimaryKey(u.getShopid());
				if (parent != null && parent.getUsername() != null) {
					u.setParentid(parent.getUsername());
				} else {
					u.setParentid("未知");
				}

				if (shop != null && shop.getUsername() != null) {
					u.setShopid(shop.getUsername());
				} else {
					u.setShopid("未知");
				}

				u.setShopnum(usersService.countByShopIDNotAll(u.getId(), null, null, null, null));
				u.setSubnum(subscribersService.countByShopidNotAll(u.getId(), null, null, null, null, null, null, null, null, null));

				List<DialogBox> dialogBox = dialogBoxService.selectByParentId(u.getId());
				if (dialogBox != null && dialogBox.size() > 0){
					u.setDialogboxid(dialogBox.get(0).getId());
				}

				//设置商家总业绩
				u.setTotalMoney(iSelfOrderService.getTotalMoney(u));
			}

			Map<String, Object> map = requestUtils.getUserData();
			request.setAttribute("selfOrderTotalShopId", map.get("shopId"));
			request.setAttribute("totalMoney", iSelfOrderService.getTotalMoney(user));
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_shangjiaguanli");
			request.setAttribute("auth", 4);
			request.setAttribute("list", users);
			request.setAttribute("adminUser", user);
			request.setAttribute("adminUserId", adminUserId);
			return modelAndView;
		}
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		logsService.insert(CommContent.TYPE_SYS, CommContent.LOGS_READ + "流量代理商信息", usernamesys, true,
				IP.getIpAddr(request));
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}

	// 搜索商家
	@RequestMapping("/selectShop")
	public ModelAndView selectShop(HttpServletRequest request, String id, String username, Integer datastatus,
			Integer curPage, String shopid, String parentid) throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		if (id == null) {
			if (request.getSession().getAttribute("id") != null) {
				id = (String) request.getSession().getAttribute("id");
			}
		}
		if (username == null) {
			if (request.getSession().getAttribute("username") != null) {
				username = (String) request.getSession().getAttribute("username");
			}
		}
		if (shopid == null) {
			if (request.getSession().getAttribute("shopid") != null) {
				shopid = (String) request.getSession().getAttribute("shopid");
			}
		}
		if (parentid == null) {
			if (request.getSession().getAttribute("parentid") != null) {
				parentid = (String) request.getSession().getAttribute("parentid");
			}
		}
		if (datastatus == null) {
			if (request.getSession().getAttribute("datastatus") != null) {
				datastatus = (Integer) request.getSession().getAttribute("datastatus");
			} else {
				datastatus = 0;
			}
		}

		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		// 登陆商家
		Users user = usersService.selectByPrimaryKey(adminUserId);
		List<Users> users = null;
		//choose = 2 默认分页查询
		Integer choose = 2;
		if(adminUsergrade < 2) {
			if(id != null &&! id.equals("") || username != null &&! username.equals("") || shopid != null &&! shopid.equals("")||
					parentid != null &&! parentid.equals("") || datastatus != 0  ) {
				choose = 1;
			}
		}

		if(adminUsergrade == 2) {
			if(id != null &&! id.equals("") || username != null &&! username.equals("") ||
				datastatus != 0  ) {
				choose = 1;
			}
		}
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();

		String sUserName = null;
		String pId = null;
	    String sId = null;
	    Boolean sDatastatus = null;

	    if(username !=null && !username.equals("")) {
	    	sUserName = username;
	    }
	    if(parentid !=null && !parentid.equals("") && !parentid.equals("undefined")) {
//	    	List<Users> pIds =usersService.selectByUserName(parentid);
//	    	if(pIds !=null && pIds.size()>0) {
//	    		pId = pIds.get(0).getId();
//	    	}
	    	pId = parentid;
	    }
		if(shopid !=null && !shopid.equals("") && !shopid.equals("undefined")) {
//			List<Users> sIds =usersService.selectByUserName(shopid);
//			if(sIds !=null && sIds.size()>0) {
//				sId = sIds.get(0).getId();
//			}
			sId = shopid;
		}
		if (datastatus != null) {
			// 判断当前可用性
			switch (datastatus) {
			case 2:
				sDatastatus = false;
				break;
			case 1:
				sDatastatus = true;
				break;
			case 0:
				break;
			}
		}

		// 条件筛选
		if(adminUsergrade == 0) {
			page.setCurrentPageNo(curPage);
			page.setRecordTotal(usersService.countByAll(sUserName, pId, sId, sDatastatus));
			users = usersService.selectByAll(page, sUserName, pId, sId, sDatastatus);
		}

		if(adminUsergrade == 1) {
			page.setCurrentPageNo(curPage);
			page.setRecordTotal(usersService.countByParentID(adminUserId, sUserName, pId, sId, sDatastatus));
			users = usersService.selectByParentID(page, adminUserId, sUserName, pId, sId, sDatastatus);
		}

		if(adminUsergrade == 2) {
			page.setCurrentPageNo(curPage);
			page.setRecordTotal(usersService.countByShopIDNotAll(adminUserId, sUserName, pId, sId, sDatastatus));
			users = usersService.selectByShopIDNotAll(page, adminUserId, sUserName, pId, sId, sDatastatus);
		}

		System.out.println(choose);
		if(choose == 2) {
			switch (adminUsergrade) {
			case 0:
				page.setCurrentPageNo(curPage);
				page.setRecordTotal(usersService.countByAll(null, null, null, null));
				users = usersService.selectByAll(page, null, null, null, null);
				break;
			case 1:
				// 查询OEMID下的用户
				// 查询OEM下的商户以及OEM
				// 查询OEM id
				page.setCurrentPageNo(curPage);
				page.setRecordTotal(usersService.countByParentID(adminUserId, null, null, null, null));
				users = usersService.selectByParentID(page, adminUserId, null, null, null, null);
				break;
			case 2:
				//当前商家的上级oem
				page.setCurrentPageNo(curPage);
				page.setRecordTotal(usersService.countByShopIDNotAll(adminUserId, null, null, null, null));
				users = usersService.selectByShopIDNotAll(page, adminUserId, null, null, null, null);
				break;
			}
		}
		if (users != null) {
			for (Users u : users) {
				DecimalFormat df = new DecimalFormat("#0.00");
				// 设置系统消耗
				BigDecimal xh = null;
				if (u.getRole() == 1) {
					xh = callbackinfoesService.sumOmoneyParentID(u.getId());
				} else if (u.getRole() == 2) {
					xh = callbackinfoesService.sumOmoneyShopID(u.getId());
				}
				if (xh == null) {
					u.setKeypath("0");
				} else {
					u.setKeypath(df.format(xh) + "");
				}
				u.setBlance(new BigDecimal(df.format(u.getBlance())));
				u.setPassword(PasswordUtils.decodePWD(u.getPassword()));// 设置明文密码
				// 设置OEM以及所属商家的账户名
				Users parent = usersService.selectByPrimaryKey(u.getParentid());
				Users shop = usersService.selectByPrimaryKey(u.getShopid());
				if (parent != null && parent.getUsername() != null) {
					u.setParentid(parent.getUsername());
				} else {
					u.setParentid("未知");
				}

				if (shop != null && shop.getUsername() != null) {
					u.setShopid(shop.getUsername());
				} else {
					u.setShopid("未知");
				}

				u.setShopnum(usersService.countByShopIDNotAll(u.getId(), null, null, null, null));
				u.setSubnum(subscribersService.countByShopidNotAll(u.getId(), null, null, null, null, null, null, null, null, null));

				List<DialogBox> dialogBox = dialogBoxService.selectByParentId(u.getId());
				if (dialogBox != null && dialogBox.size() > 0){
					u.setDialogboxid(dialogBox.get(0).getId());
				}

				//设置商家总业绩
				u.setTotalMoney(iSelfOrderService.getTotalMoney(u));
			}
			Map<String, Object> map = requestUtils.getUserData();
			request.setAttribute("selfOrderTotalShopId", map.get("shopId"));
			request.setAttribute("totalMoney", iSelfOrderService.getTotalMoney(user));
			/*
			 * 设置分页
			 */
			request.setAttribute("totlenum", page.getRecordTotal()); // 分页
			request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中

			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_shangjiaguanli");
			request.setAttribute("datastatus", datastatus);
			request.setAttribute("shopid", shopid);
			request.setAttribute("parentid", parentid);
			request.setAttribute("username", username);
			request.setAttribute("list", users);
			request.setAttribute("adminUser", user);
			request.setAttribute("adminUserId", adminUserId);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}

	// 下级商家跳转
	@RequestMapping("/content_xiajishangjiaguanli")
	public ModelAndView nextIndex(HttpServletRequest request,String id) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();

//		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = id;
		List<Users> users = null;
		page.setCurrentPageNo(1);
		page.setRecordTotal(usersService.countByShopIDNotAll(adminUserId, null, null, null, null));
		users = usersService.selectByShopIDNotAll(page, adminUserId, null, null, null, null);
		/*
		 * 设置分页
		 */
		request.setAttribute("totlenum", page.getRecordTotal()); // 分页
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
		if (users != null) {
			for (Users u : users) {
				DecimalFormat df = new DecimalFormat("#0.00");
				// 设置系统消耗
				BigDecimal xh = null;
				if (u.getRole() == 1) {
					xh = callbackinfoesService.sumOmoneyParentID(u.getId());
				} else if (u.getRole() == 2) {
					xh = callbackinfoesService.sumOmoneyShopID(u.getId());
				}
				if (xh == null) {
					u.setKeypath("0");
				} else {
					u.setKeypath(df.format(xh) + "");
				}
				u.setBlance(new BigDecimal(df.format(u.getBlance())));
				u.setPassword(PasswordUtils.decodePWD(u.getPassword()));// 设置明文密码
				// 设置OEM以及所属商家的账户名
				Users parent = usersService.selectByPrimaryKey(u.getParentid());
				Users shop = usersService.selectByPrimaryKey(u.getShopid());
				if (parent != null && parent.getUsername() != null) {
					u.setParentid(parent.getUsername());
				} else {
					u.setParentid("未知");
				}

				if (shop != null && shop.getUsername() != null) {
					u.setShopid(shop.getUsername());
				} else {
					u.setShopid("未知");
				}

				u.setShopnum(usersService.countByShopIDNotAll(u.getId(), null, null, null, null));
				u.setSubnum(subscribersService.countByShopidNotAll(u.getId(), null, null, null, null, null, null, null, null, null));

				List<DialogBox> dialogBox = dialogBoxService.selectByParentId(u.getId());
				if (dialogBox != null && dialogBox.size() > 0){
					u.setDialogboxid(dialogBox.get(0).getId());
				}
			}
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_xiajishangjiaguanli");
			request.setAttribute("datastatus", 4);
			request.setAttribute("list", users);
			request.setAttribute("id", id);
			request.setAttribute("adminUserId", adminUserId);
			return modelAndView;
		}
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		logsService.insert(CommContent.TYPE_SYS, CommContent.LOGS_READ + "流量代理商信息", usernamesys, true,
				IP.getIpAddr(request));
		modelAndView.setViewName("jsp/content_xiajishangjiaguanli");
		return modelAndView;
	}

	// 商家下级商家
	@RequestMapping("/selectNextShop")
	public ModelAndView selectNextShop(HttpServletRequest request, String id, String username, Integer datastatus,
			Integer curPage, String shopid, String parentid) throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		if (id == null) {
			if (request.getSession().getAttribute("id") != null) {
				id = (String) request.getSession().getAttribute("id");
			}
		}
		if (username == null) {
			if (request.getSession().getAttribute("username") != null) {
				username = (String) request.getSession().getAttribute("username");
			}
		}
		if (shopid == null) {
			if (request.getSession().getAttribute("shopid") != null) {
				shopid = (String) request.getSession().getAttribute("shopid");
			}
		}
		if (parentid == null) {
			if (request.getSession().getAttribute("parentid") != null) {
				parentid = (String) request.getSession().getAttribute("parentid");
			}
		}
		if (datastatus == null) {
			if (request.getSession().getAttribute("datastatus") != null) {
				datastatus = (Integer) request.getSession().getAttribute("datastatus");
			} else {
				datastatus = 0;
			}
		}

//		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = id;
		List<Users> users = null;
		//choose = 2 默认分页查询
		Integer choose = 2;
		if(username != null &&! username.equals("") || shopid != null &&! shopid.equals("")||
				parentid != null &&! parentid.equals("") || datastatus != 0  ) {
			choose = 1;
		}


		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();

		String sUserName = null;
		String pId = null;
	    String sId = null;
	    Boolean sDatastatus = null;

	    if(username !=null && !username.equals("")) {
	    	sUserName = username;
	    }
	    if(parentid !=null && !parentid.equals("") && !parentid.equals("undefined")) {
//	    	List<Users> pIds =usersService.selectByUserName(parentid);
//	    	if(pIds !=null && pIds.size()>0) {
//	    		pId = pIds.get(0).getId();
//	    	}
	    	pId = parentid;
	    }
		if(shopid !=null && !shopid.equals("") && !shopid.equals("undefined")) {
//			List<Users> sIds =usersService.selectByUserName(shopid);
//			if(sIds !=null && sIds.size()>0) {
//				sId = sIds.get(0).getId();
//			}
			sId = shopid;
		}
		if (datastatus != null) {
			// 判断当前可用性
			switch (datastatus) {
			case 2:
				sDatastatus = false;
				break;
			case 1:
				sDatastatus = true;
				break;
			case 0:
				break;
			}
		}

		// 条件筛选
		page.setCurrentPageNo(curPage);
		page.setRecordTotal(usersService.countByShopIDNotAll(adminUserId, sUserName, pId, sId, sDatastatus));
		users = usersService.selectByShopIDNotAll(page, adminUserId, sUserName, pId, sId, sDatastatus);

		System.out.println(choose);
		if(choose == 2) {
			//当前商家的上级oem
			page.setCurrentPageNo(curPage);
			page.setRecordTotal(usersService.countByShopIDNotAll(adminUserId, null, null, null, null));
			users = usersService.selectByShopIDNotAll(page, adminUserId, null, null, null, null);
		}
		if (users != null) {
			for (Users u : users) {
				DecimalFormat df = new DecimalFormat("#0.00");
				// 设置系统消耗
				BigDecimal xh = null;
				if (u.getRole() == 1) {
					xh = callbackinfoesService.sumOmoneyParentID(u.getId());
				} else if (u.getRole() == 2) {
					xh = callbackinfoesService.sumOmoneyShopID(u.getId());
				}
				if (xh == null) {
					u.setKeypath("0");
				} else {
					u.setKeypath(df.format(xh) + "");
				}
				u.setBlance(new BigDecimal(df.format(u.getBlance())));
				u.setPassword(PasswordUtils.decodePWD(u.getPassword()));// 设置明文密码
				// 设置OEM以及所属商家的账户名
				Users parent = usersService.selectByPrimaryKey(u.getParentid());
				Users shop = usersService.selectByPrimaryKey(u.getShopid());
				if (parent != null && parent.getUsername() != null) {
					u.setParentid(parent.getUsername());
				} else {
					u.setParentid("未知");
				}

				if (shop != null && shop.getUsername() != null) {
					u.setShopid(shop.getUsername());
				} else {
					u.setShopid("未知");
				}

				u.setShopnum(usersService.countByShopIDNotAll(u.getId(), null, null, null, null));
				u.setSubnum(subscribersService.countByShopidNotAll(u.getId(), null, null, null, null, null, null, null, null, null));

				List<DialogBox> dialogBox = dialogBoxService.selectByParentId(u.getId());
				if (dialogBox != null && dialogBox.size() > 0){
					u.setDialogboxid(dialogBox.get(0).getId());
				}
			}

			/*
			 * 设置分页
			 */
			request.setAttribute("totlenum", page.getRecordTotal()); // 分页
			request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中

			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_xiajishangjiaguanli");
			request.setAttribute("datastatus", datastatus);
			request.setAttribute("shopid", shopid);
			request.setAttribute("parentid", parentid);
			request.setAttribute("username", username);
			request.setAttribute("id", id);
			request.setAttribute("list", users);
			request.setAttribute("adminUserId", adminUserId);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
}
